/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.beautycenter.DAO;

import br.com.beautycenter.bean.Cliente;
import br.com.beautycenter.bean.Digital;
import br.com.beautycenter.hibernateUtil.DaoBase;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.NoResultException;
import javax.persistence.Query;

/**
 *
 * @author Magelinha
 */
public class PersisteDigital extends DaoBase<Digital> { 
    public List<Digital> retornarDigitais(){
        try {
            return findAll();
        } catch (Exception ex) {
            return new ArrayList<Digital>();
        }
    }
    
    public Digital retornarDigitalPeloId(Digital digital){
        return getEntityManager().find(Digital.class, digital.getIdDigital());
    }
        
    
    
    public List<Digital> retornarDigitaisPeloCliente(Cliente cliente){
        Query query = getEntityManager().createQuery("FROM Digital d WHERE d.cliente.idPessoa=:id");
        query.setParameter("id", cliente.getIdPessoa());
        try{
            return query.getResultList();
        }catch(NoResultException ex){
            return new ArrayList<Digital>();
        }
    }
    
    public boolean removerDigitaisPeloCliente(Cliente cliente){
        Query query = getEntityManager().createQuery("DELETE FROM Digital d WHERE d.cliente.idPessoa=:id");
        query.setParameter("id", cliente.getIdPessoa());
        try{
            query.executeUpdate();
            return true;
        }catch(Exception ex){
            ex.printStackTrace();
            return false;
        }
    }
    
    public void salvar(Digital digital){
        save(digital);
    }
    public void deletar(Digital digital){
        try{
            delete(digital);
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
    
    public void atualizar(Digital digital){
        update(digital);
    }
}



